using System;
using System.Drawing;
using System.IO;
using System.Timers;
using System.Windows.Forms;
using ZO.SmartCore.Plugins;
using ZO.SmartCore.Windows.Forms;
using ZO.SmartMessenger.Plugins;
using ZO.SmartMessenger.UI.Properties;
using Timer=System.Timers.Timer;

namespace ZO.SmartMessenger.UI
{
    /// <summary>
    /// Login Form
    /// </summary>
    public partial class LoginForm : SmartForm
    {

        private Timer timer = new Timer(250);

        private delegate void AnimatePictureBox();


        /// <summary>
        /// Displays the account controls.
        /// </summary>
        private void AnimateLogo()
        {
            // TODO Update this procedure.

            //LogoPictureBox.
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LoginForm"/> class.
        /// </summary>
        public LoginForm()
        {
            InitializeComponent();


            timer.AutoReset = true;

            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        }

        /// <summary>
        /// Handles the Elapsed event of the timer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.Timers.ElapsedEventArgs"/> instance containing the event data.</param>
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (LogoPictureBox.InvokeRequired)
            {
                LogoPictureBox.Invoke(new AnimatePictureBox(this.AnimateLogo));

            }
            else
            {
                this.AnimateLogo();
            }

        }

        /// <summary>
        /// Displays the account controls.
        /// </summary>
        /// <param name="enable">if set to <c>true</c> enable.</param>
        private void DisplayAccountControls(bool enable)
        {
            this.AccountComboBox.Enabled = enable;
            this.PasswordTextBox.Enabled = enable;
        }

        private void SignInButton_Click(object sender, EventArgs e)
        {

            //timer.Start();

            bool error = false;

            string text = "";
            if (AccountComboBox.SelectedIndex >= 0)
            {
                text = (string)AccountComboBox.Items[AccountComboBox.SelectedIndex];

                if (MessengerHelper.Accounts.Contains(text))
                {
                    if (String.IsNullOrEmpty(MessengerHelper.Accounts[text].UserName))
                    {
                        error = true;
                    }
                }
                else
                {
                    error = true;
                }
            }
            else
            {
                error = true;
            }

            if (String.IsNullOrEmpty(PasswordTextBox.Text.Trim()))
            {
                error = true;

             }

             if (error)
             {
                 MessageBox.Show("You must enter an ID or Password", "Sign-In Problem",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return;
             }

            if (MessengerHelper.Accounts.Contains(text))
            {

                Account account = MessengerHelper.Accounts[text];

                if (account != null)
                {
                    ProtocolPlugin protocol = account.Protocol;

                    if (protocol != null)
                    {
                        protocol.Username = account.UserName;
                        protocol.Password = PasswordTextBox.Text.Trim();
                        //protocol.Username = "zoey";
                        //protocol.Password = "voidmain"; 
                        //protocol.Server = "jabber.org";
                        protocol.ConnectAsync();
                    }
                }
            }

            this.Close();


            //ProtocolPlugin plugin = A

        }

        /// <summary>
        /// Loads the accounts.
        /// </summary>
        private void LoadAccounts()
        {

            this.SuspendLayout();
            this.AccountComboBox.SuspendLayout();

            
            AccountComboBox.Items.Clear();
            PasswordTextBox.Clear();

            foreach (Account account in MessengerHelper.Accounts)
            {
                if (!account.Login)
                {
                    AccountComboBox.Items.Add(account.AccountName);
                }
            }

            if (AccountComboBox.Items.Count != 0)
            {
                DisplayAccountControls(true);
            }
            else
            {
                DisplayAccountControls(false);
            }

            this.LogoPictureBox.Image = Resources.chat;

            this.AccountComboBox.ResumeLayout(true);

            this.ResumeLayout(true);

   

        }
        /// <summary>
        /// Handles the Load event of the LoginForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void LoginForm_Load(object sender, EventArgs e)
        {

            LoadAccounts();

        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the AccountComboBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void AccountComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text = (string)AccountComboBox.Items[AccountComboBox.SelectedIndex];

            if (MessengerHelper.Accounts.Contains(text))
            {
                PasswordTextBox.Text = MessengerHelper.Accounts[text].Password;

                PluginImage pluginImage = MessengerHelper.Accounts[text].Protocol.Image;

                if (pluginImage != null)
                {
                    Image image = Image.FromStream(new MemoryStream(pluginImage.GetBytes()));

                    if (image != null)
                    {
                        LogoPictureBox.Image = image;
                    }
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the AccountsButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void AccountsButton_Click(object sender, EventArgs e)
        {
            AccountManager manager = new AccountManager();

            manager.ShowDialog(this);

            if (manager.DialogResult == DialogResult.OK)
            {
                LoadAccounts();
            }

        }
    }
}